package com.powernode.controller;

import com.alibaba.fastjson.JSON;
import com.powernode.constant.QueueConstant;
import com.powernode.dto.SmsDto;
import com.powernode.model.WxMsgModel;
import com.powernode.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 内容:
 * 作者: cy
 */
@RestController
@RequestMapping("p/sms")
@Api(tags = "发送消息接口")
public class SendController {

   @Autowired
   private UserService userService;
   @Autowired
   private RabbitTemplate rabbitTemplate;


   @PostMapping("send")
   @ApiOperation("发送手机验证码")
   public ResponseEntity<String> sendSms(@RequestBody SmsDto smsDto) {
      userService.sendSms(smsDto);
      return ResponseEntity.ok("短信发送成功，请稍后去手机查看");
   }

   @PostMapping("savePhone")
   @ApiOperation("保存手机号")
   public ResponseEntity<String> savePhone(@RequestBody SmsDto smsDto) {
      userService.savePhone(smsDto);
      return ResponseEntity.ok("保存成功");
   }

   @GetMapping("testWx")
   public ResponseEntity<String> testWx(String openId) {
      WxMsgModel wxMsgModel = new WxMsgModel();
      wxMsgModel.setToUser(openId);
      wxMsgModel.setTopColor("#173177");
      wxMsgModel.setTemplateId("hTj1QbP8h5xdkeZXzUbvZlQCO-woRvAc3IDf4mgBcsQ");
      wxMsgModel.setUrl("http://www.bilibili.com");
      HashMap<String, Map<String, String>> map = new HashMap<>(8);
      map.put("time", WxMsgModel.build(new Date().toLocaleString(), "#173179"));
      map.put("goods", WxMsgModel.build("B站大会员", "#173179"));
      map.put("money", WxMsgModel.build("25.00", "#173179"));
      map.put("yue", WxMsgModel.build("49,557.00", "#173179"));
      wxMsgModel.setData(map);
      // 发送
      rabbitTemplate.convertAndSend(QueueConstant.WX_SEND_QUEUE, JSON.toJSONString(wxMsgModel));
      return ResponseEntity.ok("发送成功");
   }

}
